
'''
模拟扫码枪 获取保险盒（大+小） pn sn码
'''
import threading
import time


class Sim_SMClient:
    def __init__(self,engine=None):
        pass
        # self.out = []
        self.scan_str = ''
        # self.thread_handle = threading.Thread(
        #     target=self.thread_fun, daemon=True)
        # self.thread_handle.start()
        self.is_update = False
        self.engine = engine
    def scan2pnsn(self, scan_str):
        # 解码策略  在ui分别做了
        # pn_str, sn_str = scan_str.split(' ')
        # DZUPDZUP1SV0003.
        # DZUP1SV0003.
        # DZUP1SV0003.
        # DZUY1SV00055
        # if len(scan_str)>12:
        if len(scan_str)<4:
            pn_str = 'PnDefault'
            sn_str = 'SnDefault'
        else:
            pn_str = scan_str[:4]
            sn_str = scan_str[4:]
        return pn_str, sn_str

    # def get_pnsn_thread(self):
    #     scan_str = 'pn1 sn1'
    #     pn_str, sn_str = self.scan2pnsn(scan_str)
    #     while True:

    def get_pnsn(self): # 获取条码
        try:
            scan_decode = self.scan2pnsn(self.scan_str)
        except:
                scan_decode = 'PnDefault', 'SnDefault'
        # scan_str = 'pn1 sn1'
        return scan_decode

    def thread_fun(self):
        while True:
            scan_str = '82111-ORS20 1T70002'
            # scan_str2 = 'pnxx snxx'
            scan_str2 = '82111-ORS20 1T70002'
            self.scan_str = scan_str if self.scan_str == scan_str2 else scan_str2 # todo 更新判断
            time.sleep(10) # s

    def updata_scan_str(self, scan_from_ui_str): # 从ui编辑框获取; ui调用
        if self.engine:
            is_update = self.engine.service_isAllStepDone_dict['p_left'] and self.engine.service_isAllStepDone_dict['p_right']
            if is_update:
                self.scan_str = scan_from_ui_str
                self.engine.ui_handle.showPnSnSignal.emit([self.scan_str])
                self.engine.ui_handle.changeSignal.emit([f'当前扫码结果 {self.scan_str}'])
            else:
                self.engine.ui_handle.changeSignal.emit([f'未完成全部步骤 不可更新扫码值'])
        else:
            self.scan_str = scan_from_ui_str

def test_Sim_SMClient():
    smc = Sim_SMClient()
    snpn = smc.get_pnsn()
    print(snpn)

if __name__ == '__main__':
    test_Sim_SMClient()

    # DZUP1SV0000000000000000000000000000000000000000000000000000000000000000DZUP1SV0003.
    # DZUP1SV0003.
    # DZUPDZUP1SV0003.
    # DZUP1SV0003.
    # DZUP1SV0003.
    # DZUY1SV00055
    # DZUY1SV00055
    # DZUY1SV00055
    # DZUY1SV00055
    # DZUY1SV00055
    # DZUY1SV00055
    # DZUY1SV00055
    # DZUY1SV00055
    # DZUY1SV0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000DZUY1SV00055
    # DZUY1SV00055
    # DZUY1SV00055